package com.hzb.LNReader;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;

public class SettingActivity extends Activity {
	int getInt(int id) {
		EditText txt = (EditText) findViewById(id);
		return Integer.parseInt(txt.getText().toString());
	}
	String getStr(int id) {
		EditText txt = (EditText) findViewById(id);
		return txt.getText().toString();
	}
	void setInt(int id, int value) {
		EditText txt = (EditText) findViewById(id);
		txt.setText(Integer.toString(value));
	}
	void setStr(int id, String value) {
		EditText txt = (EditText) findViewById(id);
		txt.setText(value);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settinglayout);
		setInt(R.id.confViewBufferHeight, ReaderContext.viewBufferHeight);
		setInt(R.id.confViewBufferFillLimit, ReaderContext.fillLineCount);
		setInt(R.id.confBufferSize, ReaderContext.lineBufferSize);
		setInt(R.id.confFontSize, ReaderContext.fontSize);
		setStr(R.id.confFontColor, Integer.toHexString(ReaderContext.fontColor));
		
		setStr(R.id.confRootDir, ReaderContext.rootFolder);
		setInt(R.id.confBackSeekSize, ReaderContext.backSeekSize);
		setInt(R.id.confBufferFillLmit, ReaderContext.readLineLimit);
		setInt(R.id.confStartFillLine, ReaderContext.startFillLine);
		setInt(R.id.confScreenTimeout, ReaderContext.screenTimeout);
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		Intent intent = new Intent(this, ReaderActivity.class);
		startActivity(intent);
		finish();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		try {
			ReaderContext.viewBufferHeight = getInt(R.id.confViewBufferHeight);
			ReaderContext.fillLineCount = getInt(R.id.confViewBufferFillLimit);
			ReaderContext.lineBufferSize = getInt(R.id.confBufferSize);
			ReaderContext.fontSize = getInt(R.id.confFontSize);
			ReaderContext.fontColor = (int)Long.parseLong(getStr(R.id.confFontColor), 16);
			
			ReaderContext.rootFolder = getStr(R.id.confRootDir);
			if (!ReaderContext.rootFolder.endsWith("/")) ReaderContext.rootFolder += '/';
			ReaderContext.backSeekSize = getInt(R.id.confBackSeekSize);
			ReaderContext.readLineLimit = getInt(R.id.confBufferFillLmit);
			ReaderContext.startFillLine = getInt(R.id.confStartFillLine);
			ReaderContext.screenTimeout = getInt(R.id.confScreenTimeout);
		} catch (Exception e) {
			Log.e("SettingActivity", "Error setting config value", e);
		}
		finish();
	}
}
